package geektime.spring.springbucks;

import geektime.spring.springbucks.model.Coffee;
import geektime.spring.springbucks.service.CoffeeService;
import io.lettuce.core.ReadFrom;
import java.util.Optional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.data.redis.LettuceClientConfigurationBuilderCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@Slf4j
@EnableTransactionManagement
@SpringBootApplication
@EnableJpaRepositories
public class SpringbucksApplication  {

  @Autowired
  private CoffeeService coffeeService;

  public static void main(String[] args) {
    SpringApplication.run(SpringbucksApplication.class, args);
  }

    @Bean
  public LettuceClientConfigurationBuilderCustomizer customizer() {
    return builder -> builder.readFrom(ReadFrom.MASTER_PREFERRED);
  }


//    public LettuceClientConfigurationBuilderCustomizer customizer() {
//     return new LettuceClientConfigurationBuilderCustomizer() {
//        @Override
//        public void customize(LettuceClientConfigurationBuilder builder) {
//          builder.readFrom(ReadFrom.MASTER_PREFERRED);
//        }
//      };
//  }



  @Bean
  public RedisTemplate<String,Coffee> redisTemplate(RedisConnectionFactory redisConnectionFactory){
    RedisTemplate<String,Coffee> template = new RedisTemplate<>();
    template.setConnectionFactory(redisConnectionFactory);
    return template;
  }

  public void run(ApplicationArguments args) throws Exception {
    Optional<Coffee> c = coffeeService.findOneCoffee("mocha");
    log.info("Coffee {}", c);

    for (int i = 0; i < 5; i++) {
      c = coffeeService.findOneCoffee("mocha");
    }

    log.info("Value from Redis: {}", c);
  }
}
